package com.vanzo.talkie;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.vanzo.bean.Channel;
import com.vanzo.talkie2.R;

import org.greenrobot.greendao.annotation.Id;

import java.util.List;

/**
 * Created by tisen on 2018/1/22.
 */

public class ChannelAdapter extends RecyclerView.Adapter<ChannelAdapter.ChannelItem> {

    private static final String TAG = ChannelAdapter.class.getSimpleName();


    private Context context;
    private List<Channel> channels;
    private ItemOnClickListener onClickListener = null;
    private Application application;

    public ChannelAdapter(Context context, Application application) {
        this.context = context;
        this.application = application;
        this.channels = application.getChannels();
    }

    public void setOnClickListener(ItemOnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setChannels(List<Channel> channels) {
        this.channels = channels;
        notifyDataSetChanged();
    }

    @Override
    public ChannelItem onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ChannelItem(LayoutInflater.from(parent.getContext()).inflate(R.layout.view_channel_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ChannelItem holder, final int position) {
        final Channel channel = getChannelbByPosition(position);
        holder.getTitle().setText(channel.getTitle());
        holder.getTfv().setText(context.getString(R.string.tfv_text_value) + channel.getTfv());
        holder.getRfv().setText(context.getString(R.string.rfv_text_value) + channel.getRfv());
        holder.getCxcss().setText(channel.getCxcss());
        holder.getSq().setText(context.getString(R.string.sq_text_value) + channel.getSq());
        holder.getUsed().setVisibility(channel.getId().equals(application.getUsedId()) ? View.VISIBLE : View.INVISIBLE);
        holder.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    onClickListener.onClick(channel.getId());
                }
            }
        });
        holder.getRoot().setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onClickListener != null) {
                    onClickListener.onLongClick(channel.getId());
                }
                return false;
            }
        });
    }

    public Channel getChannelbByPosition(int position) {
        return channels.get(position);
    }

    public void setUsed(Long id) {
        application.setUsedId(id);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return channels.size();
    }

    class ChannelItem extends RecyclerView.ViewHolder {

        private View root;
        private TextView title;
        private TextView tfv;
        private TextView rfv;
        private TextView cxcss;
        private TextView sq;
        private ImageView used;


        public ChannelItem(View itemView) {
            super(itemView);
            root = itemView;
            title = (TextView) itemView.findViewById(R.id.channel_item_title);
            tfv = (TextView) itemView.findViewById(R.id.channel_item_tfv);
            rfv = (TextView) itemView.findViewById(R.id.channel_item_rfv);
            cxcss = (TextView) itemView.findViewById(R.id.channel_item_cxcss);
            sq = (TextView) itemView.findViewById(R.id.channel_item_sq);
            used = (ImageView) itemView.findViewById(R.id.channel_item_used);
        }

        public View getRoot() {
            return root;
        }

        public TextView getTitle() {
            return title;
        }

        public TextView getTfv() {
            return tfv;
        }

        public TextView getRfv() {
            return rfv;
        }

        public TextView getCxcss() {
            return cxcss;
        }

        public TextView getSq() {
            return sq;
        }

        public ImageView getUsed() {
            return used;
        }
    }

    interface ItemOnClickListener {
        void onClick(Long id);

        void onLongClick(Long id);
    }
}
